﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Astor.Modules.DataSources.Models;
using System.Collections.ObjectModel;
using Astor.Modules.DataSources.Services;
using Astor.UI.Framework.ViewModels;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using Astor.Modules.DataSources.Screens.Details;
using Microsoft.Practices.Unity;

namespace Astor.Modules.DataSources.Screens.List
{
    public class DataSourceListViewModel : BaseViewModel
    {
        public DataSourceListViewModel(IDataSourcesService dataSourcesService, IUnityContainer container) 
        {
            _dataSourcesService = dataSourcesService;
            _container = container;

            RegisterCommands();

            Load();
        }

        private void Load()
        {
            DataSources = _dataSourcesService.FindAll();
        }

        #region Commands
        
        public DelegateCommand AddCommand { get; private set; }
        public DelegateCommand EditCommand { get; private set; }
        public DelegateCommand DeleteCommand { get; private set; }

        public void RegisterCommands()
        {
            AddCommand = new DelegateCommand(OnAdd);
            EditCommand = new DelegateCommand(OnEdit, CanEdit);
            DeleteCommand = new DelegateCommand(OnDelete, () => SelectedDataSource != null);
        }

        public void OnAdd()
        {
            var detailsVM = _container.Resolve<DataSourceDetailsViewModel>();
            detailsVM.InitializeAdd("Add Data Source");
            _detailsRequest.Raise(detailsVM,
                vm =>
                {
                    if (vm.Confirmed)
                    {
                        _dataSourcesService.Add(vm.DataSource);
                        Load();
                    }
                });
        }

        public void OnEdit()
        {
            var detailsVM = _container.Resolve<DataSourceDetailsViewModel>();
            detailsVM.InitializeEdit("Edit Data Source", SelectedDataSource);
            _detailsRequest.Raise(detailsVM,
                vm =>
                {
                    if (vm.Confirmed)
                    {
                        _dataSourcesService.Modify(vm.DataSource);
                        Load();
                    }
                });

        }

        public void OnDelete()
        {
            _deleteRequest.Raise(new Confirmation { Content="Are you sure you wish to delete?", Title="Confirmation" },
                    confirmation =>
                    {
                        if (confirmation.Confirmed)
                        {
                            _dataSourcesService.Delete(SelectedDataSource);
                            Load();
                        }
            });


        }

        public bool CanEdit()
        {
            return (SelectedDataSource != null);
        }

        #endregion

        public IInteractionRequest DeleteRequest
        {
            get { return _deleteRequest;  }
        }

        public IInteractionRequest DetailsRequest
        {
            get { return _detailsRequest; }
        }

        public DataSource SelectedDataSource
        {
            get { return _selectedDataSource; }
            set { 
                _selectedDataSource = value;
                EditCommand.RaiseCanExecuteChanged();
                DeleteCommand.RaiseCanExecuteChanged();
            }
        }

        public ObservableCollection<DataSource> DataSources { get; private set; }

        private InteractionRequest<Confirmation> _deleteRequest = new InteractionRequest<Confirmation>();
        private InteractionRequest<DataSourceDetailsViewModel> _detailsRequest = new InteractionRequest<DataSourceDetailsViewModel>();

        private DataSource _selectedDataSource;
        private IDataSourcesService _dataSourcesService;
        private IUnityContainer _container;

    }


}
